﻿namespace ExamV5.WAPPage
{
    using ExamV5;
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class FrmLookExamUser : Page
    {
        protected Button butQuery;
        protected GridView DataGridInfo;
        protected DropDownList DDLOpertion;
        protected DropDownList DDLQuery;
        protected HtmlForm form1;
        protected HtmlHead Head1;
        private HttpCookie httpCookie_0 = null;
        private HttpCookie httpCookie_1 = null;
        private HttpCookie httpCookie_2 = null;
        private HttpCookie httpCookie_3 = null;
        protected ImageButton ImgButExit;
        private int int_0 = 0;
        protected Label labExamName;
        protected LinkButton LBEnterExam;
        protected LinkButton LBSet;
        private PublicFun publicFun_0 = new PublicFun();
        protected ScriptManager SM;
        private string string_0 = "A";
        protected TextBox txtQuery;
        protected TextBox txtTime;
        protected UpdatePanel UPPart;

        protected void butQuery_Click(object sender, EventArgs e)
        {
            this.DataGridInfo.PageIndex = 0;
            this.method_0();
        }

        protected void DataGridInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.DataGridInfo.PageIndex = e.NewPageIndex;
            this.method_0();
        }

        protected void DataGridInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.DataItemIndex == -1)
            //{
            //    this.DataGridInfo.Attributes.Add("oldValue", "#FFFFFF");
            //    this.DataGridInfo.Attributes.Add("singleValue", "#FFFFFF");
            //}
            //else
            //{
            //    e.Row.Attributes.Add("onmouseover", "this.bgColor='#F6F5F5'");
            //    if ((e.Row.DataItemIndex % 2) != 0)
            //    {
            //        e.Row.Attributes.Add("bgcolor", "#FFFFFF");
            //        e.Row.Attributes.Add("onmouseout", "this.bgColor=document.getElementById('DataGridInfo').getAttribute('oldValue')");
            //    }
            //    else
            //    {
            //        e.Row.Attributes.Add("bgcolor", "#FFFFFF");
            //        e.Row.Attributes.Add("onmouseout", "this.bgColor=document.getElementById('DataGridInfo').getAttribute('singleValue')");
            //    }
            //}
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                this.labExamName.Text = this.DataGridInfo.DataKeys[e.Row.RowIndex]["ExamName"].ToString();
                if (e.Row.Cells[3].Text.Trim().IndexOf("正考") > -1)
                {
                    DateTime time3;
                    TimeSpan span;
                    string str = this.DataGridInfo.DataKeys[e.Row.RowIndex]["JoinDate"].ToString();
                    if (!(str.Trim() != ""))
                    {
                        time3 = Convert.ToDateTime(this.DataGridInfo.DataKeys[e.Row.RowIndex]["StartTime"].ToString());
                        span = (TimeSpan) (Convert.ToDateTime(DateTime.Now.ToShortTimeString()) - time3);
                        if (span.TotalMinutes > Convert.ToInt32(this.DataGridInfo.DataKeys[e.Row.RowIndex]["ExamTime"].ToString()))
                        {
                            e.Row.Cells[3].Text = "异常退出";
                        }
                    }
                    else if (Convert.ToDateTime(str.Trim()).ToShortDateString() != DateTime.Now.ToShortDateString())
                    {
                        e.Row.Cells[3].Text = "异常退出";
                    }
                    else
                    {
                        time3 = Convert.ToDateTime(this.DataGridInfo.DataKeys[e.Row.RowIndex]["StartTime"].ToString());
                        span = (TimeSpan) (Convert.ToDateTime(DateTime.Now.ToShortTimeString()) - time3);
                        if (span.TotalMinutes > Convert.ToInt32(this.DataGridInfo.DataKeys[e.Row.RowIndex]["ExamTime"].ToString()))
                        {
                            e.Row.Cells[3].Text = "异常退出";
                        }
                    }
                }
                if (e.Row.Cells[3].Text.Trim() == "未考")
                {
                    DateTime time2 = Convert.ToDateTime(this.DataGridInfo.DataKeys[e.Row.RowIndex]["PaperStartTime"].ToString());
                    DateTime time = Convert.ToDateTime(this.DataGridInfo.DataKeys[e.Row.RowIndex]["PaperEndTime"].ToString());
                    if (DateTime.Now < time2)
                    {
                        e.Row.Cells[3].Text = "未考(<font color=red>时间未到</font>)";
                    }
                    if (DateTime.Now > time)
                    {
                        e.Row.Cells[3].Text = "未考(<font color=red>时间过期</font>)";
                    }
                }
            }
        }

        protected void DataGridInfo_PreRender(object sender, EventArgs e)
        {
            // 使用<TH>替换<TD> 
            this.DataGridInfo.UseAccessibleHeader = true;
            //HeaderRow将被<thead>包裹，数据行将被<tbody>包裹
            this.DataGridInfo.HeaderRow.TableSection = TableRowSection.TableHeader;
        }

        protected void ImgButExit_Click(object sender, ImageClickEventArgs e)
        {
            base.Response.Cookies["ExamUserCode"].Value = "";
            base.Response.Cookies["ExamUserName"].Value = "";
            base.Response.Cookies["ExamDeptName"].Value = "";
            base.Response.Cookies["ExamDeptID"].Value = "";
            base.Response.Cookies["ExamWorker"].Value = "";
            base.Response.Cookies["ExamUserSex"].Value = "";
            base.Response.Cookies["ExamUserLevel"].Value = "";
            base.Response.Cookies["ExamUserGWName"].Value = "";
            base.Response.Cookies["ExamUserGWLevel"].Value = "";
            string str = base.Request.Url.ToString();
            str = str.Substring(0, str.LastIndexOf('/') + 1);
            base.Response.Redirect(str + "FrmLogin.aspx");
        }

        protected void LBEnterExam_Click(object sender, EventArgs e)
        {
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            connection.Open();
            SqlCommand command = new SqlCommand("select a.LastDate,b.ExamTime,a.UserCode,a.PaperID,a.StartTime,a.ExamName,a.JoinDate from ExamUser a,PaperInfo b where b.ID=a.PaperID and a.ExamType='A' and a.Flag='B'", connection);
            SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
            while (reader.Read())
            {
                DateTime time;
                TimeSpan span;
                string str2 = reader["JoinDate"].ToString().Replace("&nbsp;", "");
                if (!(str2.Trim() != ""))
                {
                    time = Convert.ToDateTime(reader["StartTime"]);
                    span = (TimeSpan) (Convert.ToDateTime(DateTime.Now.ToShortTimeString()) - time);
                    if (span.TotalMinutes > Convert.ToInt32(reader["ExamTime"]))
                    {
                        this.method_1(reader["PaperID"].ToString(), reader["UserCode"].ToString(), reader["ExamName"].ToString());
                    }
                }
                else
                {
                    if (!(Convert.ToDateTime(str2.Trim()).ToShortDateString() != DateTime.Now.ToShortDateString()))
                    {
                        time = Convert.ToDateTime(reader["StartTime"]);
                        span = (TimeSpan) (Convert.ToDateTime(DateTime.Now.ToShortTimeString()) - time);
                        if (span.TotalMinutes > Convert.ToInt32(reader["ExamTime"]))
                        {
                            this.method_1(reader["PaperID"].ToString(), reader["UserCode"].ToString(), reader["ExamName"].ToString());
                        }
                        continue;
                    }
                    this.method_1(reader["PaperID"].ToString(), reader["UserCode"].ToString(), reader["ExamName"].ToString());
                }
            }
            reader.Close();
            command.Dispose();
            connection.Close();
            connection.Dispose();
            this.DataGridInfo.PageIndex = 0;
            this.method_0();
            ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BEnterExam", "alert('异常退出考场，统一结算成功！')", true);
        }

        protected void LBSet_Click(object sender, EventArgs e)
        {
            int num = 0;
            string str = "+";
            try
            {
                num = Convert.ToInt16(this.txtTime.Text.Trim());
            }
            catch
            {
                ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BTime", "alert('设置延长或缩短时间值不合法！')", true);
            }
            if (this.DDLOpertion.SelectedItem.Value == "A")
            {
                str = "+";
            }
            else
            {
                str = "-";
            }
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            connection.Open();
            SqlCommand command = null;
            string str2 = "";
            for (int i = 0; i < this.DataGridInfo.Rows.Count; i++)
            {
                CheckBox box = (CheckBox) this.DataGridInfo.Rows[i].FindControl("CheckBoxSelect");
                if (box.Checked)
                {
                    if (!(this.DataGridInfo.DataKeys[i]["ExamStatus"].ToString() != "B") || !(this.DataGridInfo.DataKeys[i]["ExamStatus"].ToString() != "G"))
                    {
                        str2 = string.Concat(new object[] { "update ExamUser set LastTime=LastTime", str, num, ",Flag='A' where UserCode='", this.DataGridInfo.Rows[i].Cells[1].Text.Trim(), "' and PaperID=", this.DataGridInfo.DataKeys[i]["PaperID"].ToString() });
                    }
                    else
                    {
                        str2 = string.Concat(new object[] { "update ExamUser set LastTime=", this.DataGridInfo.DataKeys[i]["ExamTime"].ToString(), str, num, ",Flag='A',IsSave=0,StartTime='',JoinDate='',LastDate='',EndTime='',GetOldCountFen=0,GetCountFen=0,LoginIP='' where UserCode='", this.DataGridInfo.Rows[i].Cells[1].Text.Trim(), "' and PaperID=", this.DataGridInfo.DataKeys[i]["PaperID"].ToString(), ";delete from CheckPaper where  UserCode='", this.DataGridInfo.Rows[i].Cells[1].Text.Trim(), "' and PaperID=", this.DataGridInfo.DataKeys[i]["PaperID"].ToString() });
                    }
                    command = new SqlCommand(str2 + ";insert into SysLogInfo(LogDisp,EditUser,EditUserCode) values('设置[" + this.DataGridInfo.Rows[i].Cells[1].Text.Trim() + "]考生重考！','" + this.httpCookie_1.Value + "','" + this.httpCookie_0.Value + "')", connection);
                    command.ExecuteNonQuery();
                    command.Dispose();
                }
            }
            connection.Close();
            connection.Dispose();
            this.DataGridInfo.PageIndex = 0;
            this.method_0();
            ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BReExam", "alert('设置重考操作成功！')", true);
        }

        private void method_0()
        {
            string selectCommandText = "";
            selectCommandText = "select a.JoinExamNum,c.PaperStartTime,c.PaperEndTime,a.JoinDate,a.LastDate,c.ExamTime,a.ExamType,a.StartTime,a.LoginIP,b.UserName,a.UserCode,a.PaperID,a.ExamName,a.Subject,a.Flag as ExamStatus,Flag=case a.Flag when 'A' then '未考' when 'B' then '正考'  when 'C' then '考完' when 'D' then '客观题批完' when 'E' then '批改完成' when 'G' then '异常操作' when 'Z' then '作弊标记' end from ExamUser a,UserInfo b,PaperInfo c  where a.PaperID=c.ID and a.UserCode=b.UserCode and a.PaperID=" + this.int_0;
            if (this.txtQuery.Text.Trim() != "")
            {
                selectCommandText = selectCommandText + " and " + this.publicFun_0.CheckString(this.DDLQuery.SelectedItem.Value) + " like '%" + this.publicFun_0.CheckString(this.txtQuery.Text) + "%'";
            }
            if (this.ViewState["ManagExam"].ToString() != "Y")
            {
                if (this.ViewState["AddExam"].ToString() == "Y")
                {
                    selectCommandText = selectCommandText + " and c.InputUserCode='" + this.httpCookie_0.Value + "'";
                }
                else
                {
                    selectCommandText = selectCommandText + " and c.ID=0 ";
                    ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BPower", "alert('考试监考权限未开放！')", true);
                }
            }
            else
            {
                selectCommandText = selectCommandText + " and c.ID<>0 ";
            }
            selectCommandText = selectCommandText + " order by " + this.ViewState["SortFiled"].ToString() + this.ViewState["SortType"].ToString();
            SqlConnection selectConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            SqlDataAdapter adapter = new SqlDataAdapter(selectCommandText, selectConnection);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet, "DataInfo");
            this.DataGridInfo.DataSource = dataSet.Tables["DataInfo"].DefaultView;
            this.DataGridInfo.DataBind();
            adapter.Dispose();
            selectConnection.Close();
            selectConnection.Dispose();
        }

        private void method_1(string string_1, string string_2, string string_3)
        {
            string str = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Second.ToString();
            string str2 = "E";
            double num2 = 0.0;
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            connection.Open();
            SqlCommand command = new SqlCommand("select RubricSFen,RubricDFen,RubricBFen,RubricFFen,RubricAFen,RubricCFen,RubricTFen,RubricANFen,RubricOFen,RubricJFen from CheckPaper where UserCode='" + string_2 + "' and PaperID='" + string_1 + "'", connection);
            SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
            if (reader.Read())
            {
                int num4;
                if (((((reader["RubricAFen"].ToString().Trim() == "") && (reader["RubricCFen"].ToString().Trim() == "")) && ((reader["RubricTFen"].ToString().Trim() == "") && (reader["RubricANFen"].ToString().Trim() == ""))) && ((reader["RubricOFen"].ToString().Trim() == "") && (reader["RubricJFen"].ToString().Trim() == ""))) && !(reader["RubricFFen"].ToString().Trim() != ""))
                {
                    str2 = "E";
                }
                else
                {
                    str2 = "D";
                }
                if (reader["RubricSFen"].ToString().Trim() != "")
                {
                    string[] strArray11 = reader["RubricSFen"].ToString().Trim().Split(new char[] { '|' });
                    for (num4 = 0; num4 < strArray11.Length; num4++)
                    {
                        if (strArray11[num4].Trim() != "")
                        {
                            num2 += Convert.ToDouble(strArray11[num4].Trim());
                        }
                    }
                }
                if (reader["RubricDFen"].ToString().Trim() != "")
                {
                    string[] strArray10 = reader["RubricDFen"].ToString().Trim().Split(new char[] { '|' });
                    for (num4 = 0; num4 < strArray10.Length; num4++)
                    {
                        if (strArray10[num4].Trim() != "")
                        {
                            num2 += Convert.ToDouble(strArray10[num4].Trim());
                        }
                    }
                }
                if (reader["RubricBFen"].ToString().Trim() != "")
                {
                    string[] strArray9 = reader["RubricBFen"].ToString().Trim().Split(new char[] { '|' });
                    for (num4 = 0; num4 < strArray9.Length; num4++)
                    {
                        if (strArray9[num4].Trim() != "")
                        {
                            num2 += Convert.ToDouble(strArray9[num4].Trim());
                        }
                    }
                }
                if (reader["RubricFFen"].ToString().Trim() != "")
                {
                    string[] strArray8 = reader["RubricFFen"].ToString().Trim().Split(new char[] { '|' });
                    for (num4 = 0; num4 < strArray8.Length; num4++)
                    {
                        if (strArray8[num4].Trim() != "")
                        {
                            num2 += Convert.ToDouble(strArray8[num4].Trim());
                        }
                    }
                }
                if (reader["RubricAFen"].ToString().Trim() != "")
                {
                    string[] strArray7 = reader["RubricAFen"].ToString().Trim().Split(new char[] { '|' });
                    for (num4 = 0; num4 < strArray7.Length; num4++)
                    {
                        if (strArray7[num4].Trim() != "")
                        {
                            num2 += Convert.ToDouble(strArray7[num4].Trim());
                        }
                    }
                }
                if (reader["RubricCFen"].ToString().Trim() != "")
                {
                    string[] strArray6 = reader["RubricCFen"].ToString().Trim().Split(new char[] { '|' });
                    for (num4 = 0; num4 < strArray6.Length; num4++)
                    {
                        if (strArray6[num4].Trim() != "")
                        {
                            num2 += Convert.ToDouble(strArray6[num4].Trim());
                        }
                    }
                }
                if (reader["RubricANFen"].ToString().Trim() != "")
                {
                    string[] strArray5 = reader["RubricANFen"].ToString().Trim().Split(new char[] { '|' });
                    for (num4 = 0; num4 < strArray5.Length; num4++)
                    {
                        if (strArray5[num4].Trim() != "")
                        {
                            num2 += Convert.ToDouble(strArray5[num4].Trim());
                        }
                    }
                }
                if (reader["RubricTFen"].ToString().Trim() != "")
                {
                    string[] strArray4 = reader["RubricTFen"].ToString().Trim().Split(new char[] { '|' });
                    for (num4 = 0; num4 < strArray4.Length; num4++)
                    {
                        if (strArray4[num4].Trim() != "")
                        {
                            num2 += Convert.ToDouble(strArray4[num4].Trim());
                        }
                    }
                }
                if (reader["RubricOFen"].ToString().Trim() != "")
                {
                    string[] strArray3 = reader["RubricOFen"].ToString().Trim().Split(new char[] { '|' });
                    for (num4 = 0; num4 < strArray3.Length; num4++)
                    {
                        if (strArray3[num4].Trim() != "")
                        {
                            num2 += Convert.ToDouble(strArray3[num4].Trim());
                        }
                    }
                }
                if (reader["RubricJFen"].ToString().Trim() != "")
                {
                    string[] strArray2 = reader["RubricJFen"].ToString().Trim().Split(new char[] { '|' });
                    for (num4 = 0; num4 < strArray2.Length; num4++)
                    {
                        if (strArray2[num4].Trim() != "")
                        {
                            num2 += Convert.ToDouble(strArray2[num4].Trim());
                        }
                    }
                }
                this.publicFun_0.ExecuteSql(string.Concat(new object[] { 
                    "update ExamUser set GetOldCountFen=", num2, ",GetCountFen=", num2, ",Flag='", str2, "',EndTime='", str, "',LastDate='", DateTime.Now.ToShortDateString(), "' where UserCode='", string_2, "' and PaperID='", string_1, "';insert into SysLogInfo(LogDisp,EditUser,EditUserCode) values('强行给[", string_2, 
                    "]考生交卷[", string_3, "]！','", this.httpCookie_1.Value, "','", this.httpCookie_0.Value, "')"
                 }));
            }
            else
            {
                this.publicFun_0.ExecuteSql("update ExamUser set GetOldCountFen=0,GetCountFen=0,Flag='E',EndTime='" + str + "',LastDate='" + DateTime.Now.ToShortDateString() + "' where UserCode='" + string_2 + "' and PaperID='" + string_1 + "';insert into SysLogInfo(LogDisp,EditUser,EditUserCode) values('强行给[" + string_2 + "]考生交卷[" + string_3 + "]！','" + this.httpCookie_1.Value + "','" + this.httpCookie_0.Value + "')");
            }
            reader.Close();
            command.Dispose();
            connection.Close();
            connection.Dispose();
            double num3 = 0.0;
            string values = this.publicFun_0.GetValues(string.Concat(new object[] { "select StudyFen from GradeReule where PaperID=", string_1, " and (", num2, ">=MinValue  and ", num2, "<=MaxValue)" }), "StudyFen");
            if (values.Trim() == "")
            {
                values = "0";
            }
            num3 = Convert.ToDouble(values);
            this.publicFun_0.ExecuteSql(string.Concat(new object[] { "delete  UserStudyFen where UserCode='", this.httpCookie_0.Value, "' and ExamOrStudy='E' and Formtable='PaperInfo' and FormID=", string_1, ";insert into UserStudyFen(UserCode,ExamStudyName,StudyFen,Formtable,FormID,ExamOrStudy) values('", this.httpCookie_0.Value, "','", string_3, "','", num3, "','PaperInfo','", string_1, "','E')" }));
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.httpCookie_0 = new HttpCookie("ExamUserCode");
            this.httpCookie_0 = base.Request.Cookies["ExamUserCode"];
            this.httpCookie_1 = new HttpCookie("ExamUserName");
            this.httpCookie_1 = base.Request.Cookies["ExamUserName"];
            this.httpCookie_2 = new HttpCookie("ExamUserLevel");
            this.httpCookie_2 = base.Request.Cookies["ExamUserLevel"];
            this.httpCookie_3 = new HttpCookie("ExamDeptID");
            this.httpCookie_3 = base.Request.Cookies["ExamDeptID"];
            this.int_0 = Convert.ToInt32(base.Request["PaperID"].ToString());
            if ((this.httpCookie_0 != null) && (this.httpCookie_0.Value != ""))
            {
                base.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                base.Response.Cache.SetNoStore();
                this.httpCookie_0.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_0.Value));
                this.httpCookie_1.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_1.Value));
                this.httpCookie_2.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_2.Value));
                this.httpCookie_3.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_3.Value));
                if (!base.IsPostBack)
                {
                    if (this.httpCookie_2.Value != "A")
                    {
                        this.string_0 = base.Request["Flag"].ToString().Trim();
                        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
                        connection.Open();
                        SqlCommand command = new SqlCommand("select AddExam,ManagExam  from UserPower where UserCode='" + this.httpCookie_0.Value + "'", connection);
                        SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                        if (reader.Read())
                        {
                            this.ViewState["AddExam"] = reader["AddExam"].ToString();
                            this.ViewState["ManagExam"] = reader["ManagExam"].ToString();
                        }
                        reader.Close();
                        command.Dispose();
                        connection.Close();
                        connection.Dispose();
                        this.ViewState["SortFiled"] = "UserCode";
                        this.ViewState["SortType"] = " Desc";
                        this.LBSet.Attributes.Add("onclick", @"javascript:{if(confirm('考试完成设置重考将清除考试成绩和作答记录。\n\n断电死机非法退出设置重考可继考。\n\n请再次确定将所有选中考生设置重考吗？')==false) return false;}");
                        this.method_0();
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheck", "alert('在线监考权限未开放！')", true);
                    }
                }
                else
                    ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "Render", "LayaUIFormRender();", true);
            }
            else
            {
                base.Response.Write("<script language=javascript>top.location.href='../Index.htm';self.close();</script>");
            }
        }
    }
}

